import { queryProjectSelect } from "@/api/base/bsProject";
import { disposeCompany } from "@/utils";

const project = {
  state: {
    projectList: [],
    projectTreeList: [],
    projectId: ""
  },

  mutations: {
    SET_PROJECT_LIST: (state, projectList) => {
      state.projectList = projectList;
    },
    SET_PROJECT_TREE: (state, projectList) => {
      state.projectTreeList = projectList;
    },
    SET_PROJECT_ID: (state, projectId) => {
      state.projectId = projectId;
    }
  },

  actions: {
    // 获取用户项目信息
    getProject({ commit, state }) {
      return new Promise((resolve, reject) => {
        queryProjectSelect()
          .then((res) => {
            const dataList = res.data;
            const projectOptions = dataList.map((item) => {
              return {
                value: item.projectId,
                label: item.projectName,
                ...item
                // companyName: companyName
              };
            });
            if (dataList.length > 0) {
              const projectId = dataList[0].projectId;
              commit("SET_PROJECT_ID", projectId);
            }
            resolve(res);
            commit("SET_PROJECT_LIST", projectOptions);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // // 获取用户项目信息树
    // getProjectTree({ commit, state }) {
    //   return new Promise((resolve, reject) => {
    //     listProjectTree()
    //       .then((res) => {
    //         const dataList = res.data;
    //         if (dataList.length > 0) {
    //           let projectId = state.projectId;
    //           if (!state.projectId) {
    //             if (dataList[0] && dataList[0].children && dataList[0].children.length > 0 && dataList[0].children[0] && dataList[0].children[0].projectId) {
    //               projectId = dataList[0].children[0].projectId;
    //             }
    //           }
    //           // const projectOptions = dataList.map((item) => {
    //           // const companyName = disposeCompany(item.ownerDeptId);
    //           //   return {
    //           //     value: item.projectId,
    //           //     label: item.projectName,
    //           //     ...item,
    //           //     companyName: companyName,
    //           //   };
    //           // });
    //           commit("SET_PROJECT_ID", projectId);
    //           commit("SET_PROJECT_TREE", dataList);
    //         }
    //         resolve(res);
    //       })
    //       .catch((error) => {
    //         reject(error);
    //       });
    //   });
    // },
    // 修改选中项目
    changeProject({ commit }, data) {
      commit("SET_PROJECT_ID", data);
    }
  }
};

export default project;
